﻿using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using Smartkernel.IO;
using System.Text;

namespace Smartkernel.NUnitTest.IO
{
    [Category("IO")]
    [TestFixture]
    public class SmartFileTest
    {
        [Test]
        public void Test()
        {
            //【01】写入文件测试（涉及方法：WriteText、WriteLineText）
            string path = @"C:\SmartFileTest" + DateTime.Now.Ticks.ToString() + ".txt";
            for (int i = 0; i < 5; i++)
            {
                SmartFile.WriteText(path, System.DateTime.Now.Ticks.ToString());
            }
            for (int i = 0; i < 5; i++)
            {
                SmartFile.WriteLineText(path, System.DateTime.Now.Ticks.ToString());
            }
            string guid = System.Guid.NewGuid().ToString();
            SmartFile.WriteLineText(path, guid);

            //【02】读取文件测试（涉及方法：ReadText）
            Assert.AreEqual(SmartFile.ReadText(path).Contains(guid), true);
            File.Delete(path);

            //【03】写入文件测试Encoding（涉及方法：WriteText、WriteLineText）
            path = @"C:\SmartFileTestEncoding" + DateTime.Now.Ticks.ToString() + ".txt";
            for (int i = 0; i < 5; i++)
            {
                SmartFile.WriteText(path, System.DateTime.Now.Ticks.ToString(), Encoding.GetEncoding("gb2312"));
            }
            for (int i = 0; i < 5; i++)
            {
                SmartFile.WriteLineText(path, System.DateTime.Now.Ticks.ToString(), Encoding.GetEncoding("gb2312"));
            }
            guid = System.Guid.NewGuid().ToString();
            SmartFile.WriteLineText(path, guid, Encoding.GetEncoding("gb2312"));

            //【04】读取文件测试Encoding（涉及方法：ReadText）
            Assert.AreEqual(SmartFile.ReadText(path, Encoding.GetEncoding("gb2312")).Contains(guid), true);
            File.Delete(path);

            //【05】在指定的目录中查找某个文件，判断是否存在，如果存在，则返回文件路径（涉及方法：Exists）
            List<string> list = null;
            SmartFile.Exists("notepad.exe", @"C:\WINDOWS", SearchOption.TopDirectoryOnly, out list);
            Assert.AreEqual(list.Count, 1);

            //【06】分割文件测试（涉及方法：Split、Unite）
            string sourceFile = @"C:\SmartFileTestSplit.txt";
            if (!File.Exists(sourceFile))
            {
                using (StreamWriter streamWriter = File.CreateText(sourceFile))
                {
                    for (int i = 0; i < 100000; i++)
                    {
                        streamWriter.WriteLine(DateTime.Now.Ticks.ToString());
                    }
                }
            }
            SmartFile.Split(sourceFile, sourceFile, 10);
            File.Delete(sourceFile);
            SmartFile.Unite(sourceFile, sourceFile, 10);
            File.Delete(sourceFile);
            for (int i = 0; i < 10; i++)
            {
                File.Delete(sourceFile + "_" + i + "_.part");
            }

            //【07】显示明显的文件大小形式（涉及方法：FileSize）
            Console.WriteLine(SmartFile.FileSize(1024));
            Console.WriteLine(SmartFile.FileSize(1024000));
            Console.WriteLine(SmartFile.FileSize(1024 * 1024));
            Console.WriteLine(SmartFile.FileSize(1024 * 1024 * 152 * 12));

            //【07】获得项目文件列表（涉及方法：GetProjectFileList）
            //List<string> fileList = SmartFile.GetProjectFileList(@"G:\技术开发\框架开发\Smartkernel\Smartkernel\Smartkernel.csproj");
            //Assert.AreEqual(fileList.Count > 0, true);

            //【09】获得指定目录扩展名类型（涉及方法：GetExtensionsType）
            List<string> extensionList = SmartFile.GetExtensionsType(@"C:\WINDOWS\system32\IME", SearchOption.AllDirectories);
            Assert.AreEqual(extensionList.Count > 0, true);

            //【10】获得指定目录文件列表（涉及方法：GetFiles）
            List<string> files = SmartFile.GetFiles(@"C:\WINDOWS\system32\IME", SearchOption.AllDirectories);
            Assert.AreEqual(files.Count > 0, true);
        }
    }
}
